<?php
$script = 'survey';
require_once '../controller.php';
require_once '../header.php';
if(isset($_SESSION['logged_in'])) {
    
    $instructor_id = $_SESSION['logged_in'];

}


// Displaying Survey Results.
if(isset($_REQUEST['sid'])) {

    $survey_id = $_REQUEST['sid'];

    echo '<h1 align="center">Survey Results</h1>';

    $s = new Survey();
    $survey = $s->getSurvey($survey_id);

    echo '<p align="center"> Survey Name: '.$survey['name'].'</p><br>';
    
    $questions = $s->getQuestions($survey_id);
    
    if($questions){
    foreach ($questions as $i => $value) {
         $question_id[] = $value->id;
         $type[] = $value->type;
         $question_value[] = $value->question;
       }
    }
   }
    $num = sizeof($question_id);

    if(!isset($_POST['select_questions'])) {
        echo '<form name="select_ques" method ="post" >';
        echo "<input type='checkbox' id='check_all' onclick='javascript:checkAll($num);'  name='all' value='all'> View all questions <br>";

        for($i=0;$i<$num;$i++) {
        	if($type[$i]!='Section Text'){
            echo '<input type="checkbox" id="check_'.$i.'" name="que_'.$i.'" value="'.$question_id[$i].'">'.$question_value[$i].'<br>';
        }
        }
        echo ' <input type="submit" name="select_questions" value="Select Questions"> <br>';

        echo '</form>';
    }

   
    if(isset($_POST['select_questions'])) {
    	 echo "<p align='right'><a href='export.php'><font color='blue' size='8em'>Export Results</font></a></p><br><br>";
        echo '<table border="1px" cellpadding="10px"><tr>
        <td>Group Name</td>
        <td>Student Name</td>
        <td>Date</td>
        <td>Time</td>';
        $datafile = "datafile.csv";
        $handle = fopen($datafile,'w') or die("can't open file");
        fwrite($handle,'Group Name,Respondent Name,Date,Time');
        $display_questions =0;
        for($i=0;$i<$num;$i++) {

            if(isset($_POST['que_'.$i])){
                $display_questions = $display_questions +1;
                $display_responses[]=$i;
                echo  '<td>'.($i+1).'. '.$question_value[$i].'</td>';
                fwrite($handle,','.$question_value[$i]);
        }
        }
        echo '</tr>';
        fwrite($handle,"\r\n");
          
        $responses = $s->getResponses($survey_id);
        if($responses){
        foreach ($responses as $r=>$value) {
             $rid[] = $value->id;
             $member_id[] = $value->student_id;
             $member_name[] = $value->student_name;
             $date[] = $value->timestamp;
               
            }

        }else {
            echo '<p align="center"><font size = "6.2em" color ="red">No Response Available yet</font></p><br><br>';

        }
    
        $size = sizeof($rid);
        $response = new Response();
        for($j=0;$j<$size;$j++) {

            $student = new Student($member_id[$j]);
            $group_id = $student ->group;
            
            $group = new Group($group_id);
            $group_name = $group->name;
           
            $datetime= explode(" ",$date[$j]);

            echo "<tr><td>".$group_name."</td><td>".$member_name[$j]."</td><td>".$datetime[0]."</td><td>".$datetime[1]."</td>";
            fwrite($handle, $group_name.','.$member_name[$j].','.$datetime[0].','.$datetime[1]);
            for($n =0;$n<$display_questions;$n++) {
            if(isset($_POST['que_'.$display_responses[$n]])) {
               $response_value = $response->getResponseValue($rid[$j],$question_id[$display_responses[$n]]);
               echo "<td>".$response_value."</td>";
               fwrite($handle, ','.$response_value);
              }
             }  
            echo "</tr>";
            fwrite($handle, "\r\n");
        }
        echo '</table><br><br>';
        fclose($handle);

    }

require_once '../footer.php';
?>



